package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.TimeShow;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 *通缉榜系统  
	 * @author pangj
	 * 
	 */
	public class WantedListUI extends FrameUI
	{
		public static var BOX_COLOR_GOLDEN :String = "ffe600";    //金色
		public static var BOX_COLOR_ORANGE :String = "f47920";    //橙色
		public static var BOX_COLOR_PURPLE :String = "a518ff";    //紫色
		public static var BOX_COLOR_WHITE  :String = "ffffff";    //白色
		public static var BOX_COLOR_GREEN  :String = "00ff2d";    //绿色
		public static var BOX_COLOR_BLUE   :String = "00e4ff";    //蓝色
		
		public static var TASK_TRAIT_WHITE :int = 1;//白色品质任务
		public static var TASK_TRAIT_GREEN :int = 2;//绿色品质任务
		public static var TASK_TRAIT_BLUE  :int = 3;//蓝色品质任务
		public static var TASK_TRAIT_PURPLE:int = 4;//紫色品质任务
		public static var TASK_TRAIT_ORANGE:int = 5;//橙色品质任务
		public static var TASK_TRAIT_GOLDEN:int = 6;//金色品质任务
		public static var Max_TRAIT        :int = 6;//品质积分列表最大品质  用于显示品质积分窗口时计算y坐标
		
		private static var _self    :WantedListUI=null;
		private var _model           :Modulator = Modulator.instance;
		
		
		private var taskNum          :int;                 //本日可完成次数
		private var lostTime         :Number;              //刷新剩余时间 
		private var timer            :Timer;               //计时器  用于刷新时间归零后将“立即刷新”改为“免费刷新”；
		private var tasks            :Array;               //任务
		private var scoreBoxs        :Array;               //宝箱
		private var scoreX           :int;                 //积分飞入起点
		private var scoreY           :int;                 //积分飞入起点
		public var  refTime          :TimeShow;            //刷新时间倒计时	
		public  var effect           :WantedScoreEffect;
		private var isShowEffect:Boolean=false;
		//界面控件
		public var todayTimesClip    :Clip;                //本日可完成次数 文字
		public var todayTimes        :Label;			    //本日可完成次数 数值
		public var refurbishClip     :Clip;                //刷新时间 文字
		public var taskQualityScore  :Label;               //任务品质积分标签
		public var addTime           :Button;              //增加次数按钮
		public var refurbish         :Button;              //立即刷新按钮
		public var reQuality         :Button;              //刷新品质按钮
		public var taskWinBG         :Window;              //任务显示窗口
		public var scoreWinBG        :Window;              //品质积分显示窗口
		
		
		/**
		 * 获取单例
		 * 
		 */
		public static function getInstance():WantedListUI
		{
			if(_self==null)
			{
				_self=new WantedListUI();
			}
			return _self;
		}
		
		public function WantedListUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEven();
		}
		private function init():void
		{
			scoreBoxs = [];
		}
		/**
		 * 重写显示事件 
		 * 
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_DAILY_TASK_OPEN);
			super.show(space,onshow);
		}
		/**
		 * 事件初始化 
		 * 
		 */
		private function initEven():void
		{
			/*_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_DAILYTASK_LIST,getTask);
			_model.addEventListener(CustomToClientEvent.SERCER_CUSTOMMSG_DAILY_TASK_COL,getScore);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_REFRESH_OK,setBtn);*/
			addTime.addEventListener(MouseEvent.CLICK,onAddTime);
			refurbish.addEventListener(MouseEvent.CLICK,onRefurbish);
			reQuality.addEventListener(MouseEvent.CLICK,onReQuality);
		}
		/**
		 * 增加次数 
		 * 
		 */
		private function onAddTime(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_DAILY_TASK_OK,0);
		}
		/**
		 * 立即刷新
		 * 
		 */
		private function onRefurbish(evt:MouseEvent):void
		{
			if(null!=timer)
				timer.stop();
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_DAILY_TASK_JUDGE,1);
			refurbish.enabled = false;
		}
		/**
		 * 刷新品质 
		 * 
		 */
		private function onReQuality(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_DAILY_TASK_COLOR);
		}
		
		/**
		 * 获取任务信息 
		 * 
		 */
		private function getTask(evt:CustomToClientEvent):void
		{
			//移除之前的任务
			removeTask();
			//停止计时器
			if(null != refTime)
			{
				refTime.time = 0;
			}
			//从新创建任务
			tasks = new Array;
			//获取任务信息 
			/*taskNum  = evt.pList.shift();//可接任务数
			lostTime = evt.pList.shift();//刷新时间
			var length:int = evt.pList.length/3;
			for(var i:int=0;i<length && i<5;i++)
			{                                             //任务id          //任务状态         //任务品质
				var task:WantedTaskUI = new WantedTaskUI(evt.pList.shift(),evt.pList.shift(),evt.pList.shift());
				task.x=task.x+(task.windowBG.width+5)*i;
				tasks[i]=task;
				taskWinBG.addChild(tasks[i]);
			}*/
			
			//显示
			todayTimes.htmlStr = taskNum+"/10";
			refurbish.label = StaticIni.getSwfLangStr(lostTime == 0?"1000000309":"1000000204");

			//倒计时
			if(refTime==null)
			{
				refTime   = new TimeShow(lostTime);
				refTime.x = 450;
				refTime.y = 25;
				taskWinBG.addChild(refTime);
			}
			refTime.time = lostTime;
			refTime.styleColor(refTime.time==0?"00ff2d":"ff0000");
			//计时器：倒计时归零时更改按钮显示
			timer = new Timer((1+lostTime)*1000,1);
			timer.addEventListener(TimerEvent.TIMER,freeRefurbish);
			timer.start();
		}
		/**
		 * 更改按钮显示
		 * 
		 */
		private function freeRefurbish(evt:TimerEvent):void
		{
			refurbish.label = StaticIni.getSwfLangStr("1000000309");
		}

		/**
		 *移除窗口上的任务显示 
		 * 
		 */
		private function removeTask():void
		{
			if(null != tasks)
			{
				for(var i:int=0;i<tasks.length;i++)
				{
					if(null!=tasks[i].parent)
					{
						tasks[i].parent.removeChild(tasks[i]);
					}
				}
			}
		}
		/**
		 * 获取积分，初始化宝箱界面
		 * 
		 */
		private function getScore(evt:CustomToClientEvent):void
		{
			/*var color:int = evt.pList.shift();//宝箱品质颜色
			if (!((color == Max_TRAIT) || (color == Max_TRAIT-1) || color == (Max_TRAIT-2)))
				return;
			var count:int = evt.pList.shift();//完成次数
			
			if(isShowEffect)
			{
				var endX:int = this.x + this.scoreWinBG.x + WantedScoreUI(scoreBoxs[Max_TRAIT-color]).x+40;
				var endY:int = this.y + this.scoreWinBG.y + WantedScoreUI(scoreBoxs[Max_TRAIT-color]).y+15;
				effect = new WantedScoreEffect([scoreX,scoreY,endX,endY],onPlayOver);
				effect.show();
				isShowEffect = false;
			}
			if(null == scoreBoxs[Max_TRAIT-color])
			{
				var box:WantedScoreUI = new WantedScoreUI(color,count);
				box.y = box.y+(box.height+8)*(Max_TRAIT-color);
				scoreBoxs[Max_TRAIT-color] = box;
				scoreWinBG.addChild(scoreBoxs[Max_TRAIT-color]);
			}
			else
			{
				WantedScoreUI(scoreBoxs[Max_TRAIT-color]).count =count;
			}*/
		}
		public function setBtn(evt:CustomToClientEvent):void
		{
			refurbish.enabled = true;
		}
		public function setOrigin(x:int,y:int):void
		{
			isShowEffect = true;
			this.scoreX = x + this.x + this.taskWinBG.x;
			this.scoreY = y + this.y + this.taskWinBG.y;
		}
		public function onPlayOver():void
		{
			
		}
	}
}